<?php
namespace Common\Library;

class xs74{

    protected $BookArticle;
    protected $detailUrl;

    protected $id;

    public function __construct($id,$url = ''){
        $this->id = $id;
        //$url = 'http://www.5du5.net/book/2/2958/';
        if(empty($url)){
            $this->update();exit;
        }
        $this->detailUrl = $url;
        $this->addNewBook();

    }

    //添加一本新书
    protected function addNewBook(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/xs74/addbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'og:image" content="','<div class="dahengfu"><tr><td><script>');

        $listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div id="list">','<div class="dahengfu"><script type=');
        //p($getStr);exit;
        unset($getStr,$pathFile);
        //提取书籍名称
        $bookName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<h1>','</h1>');
        //提取作者
        $author = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'作&nbsp;&nbsp;&nbsp;&nbsp;者：','<p>动&nbsp;&nbsp;&nbsp;&nbsp;作');
        $author = rtrim($author,'</p>');
        //p($author);p($bookName);p($bookInfo);exit;
        if(empty($bookName) || empty($author)){
            $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
            exit('书名,作者,或为空!:'.$this->detailUrl);
        }

        //作者处理
        $author_id = D('Common/BookAuthor')->getAuthorId($author);
        if(empty($author_id)){
            $this->writeLog('作者ID错误:'.$author_id.' @@ '.$author.' @@ '.$this->detailUrl,'ERR');
            exit();
        }
        $Book = new \Common\Model\BookModel();
        $unique = md5($bookName.$author_id);
        //书本重复检测
        $book_id = $Book->checkBookRepeart($unique);
        //p($book_id);exit;
        if(empty($book_id)){//新书入库

            //提取简介
            $intro = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<div id="intro">','<div id="sidebar">');
            $intro = strip_tags($intro);
            $intro = trim($intro);
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'小说网</a> &gt; ','  &gt; ');
            //提取封面图片
            $end = mb_strpos($bookInfo,'"/>');
            $image = mb_substr($bookInfo,0,$end);
            //p($intro);exit;
            //书籍状态
            $bookStatus = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'height="150" /><span class="','"></span></div>');
            if($bookStatus == 'a'){
                $status = 'end';
            }else{
                $status = 'ing';
            }
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);
            if(empty($cate_id)){
                $this->writeLog('入库错误,没有分类:'.var_export($cate,1).'@@'.var_export(D('Common/BookCate')->getLastSql(),1),'ERR');
                exit('入库错误,没有获取到分类');
            }

            //封面图片处理
            $imagePath = \Common\Library\COM\CollectDo::doImagePath();

            $dataBook = array(
                'book_name'=>$bookName,
                'author_id'=>$author_id,
                'cate_id'=>$cate_id,
                'introduction'=>  htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$imagePath,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            $book_id = $Book->saveBook($dataBook);
            \Common\Library\COM\CollectDo::downPage('http://www.37zw.com'.$image, '.'.$imagePath);

        }
        unset($bookInfo,$start,$end);
        //新书开始入库
        if(empty($book_id)){
            $this->writeLog('入库错误,书本未入库:'.  var_export($Book->getLastSql(),1),'ERR');
            exit('入库错误,书本未入库');
        }else{
            $this->writeLog('bookinstall.新书开始入库:'.var_export($bookName,1));
        }
         unset($Book);
        //章节逐个对比采集
        $this->articleCollection($listInfo,$book_id);
    }

    //章节列表名称入库
    protected function articleCollection($listInfo,$book_id){
        $article_preg = "/<li>\s*(.+?)\s*<\/li>/i";
        preg_match_all($article_preg, $listInfo, $articles);
        $articleList = $articles[1];
        unset($listInfo,$articles,$article_preg);
        //p($listInfo);p($articles);($articleList);exit;
        if(empty($articleList)){
            $this->writeLog('章节目录为空！!@Value:'.$book_id.'@URL:'.$this->detailUrl,'ERR');
            exit('章节目录为空！');
        }
        $this->BookArticle = new \Common\Model\BookArticleModel();
        foreach ($articleList as $key=>$val){
            $this->writeLog($val.':章节入库开始!');
            $piece_num = 0;
            if(!strpos($val,'href=')){//发现篇名
                $piece_name = strip_tags($val);
                $piece_num =  \Common\Library\COM\CollectDo::getPieceNum($val);
                continue;
            }
            //获取章节名称
            $article_name = \Common\Library\COM\CollectDo::getSubstr($val,'.html" ">','</a>');
            //p($article_name);exit;
            if(empty($article_name) || empty($book_id)){
                $this->writeLog('章节名称不能为空!@Value:'.$val.'@URL:'.$this->detailUrl,'ERR');
                exit;
            }
            $unique = md5($book_id.$article_name);
            $result = $this->BookArticle->checkArtcileRepeat($unique);

            if($result){//重复章节检测
                $this->writeLog($article_name.' ====> 重复章节');
                continue;
            }

            $article_num = \Common\Library\COM\CollectDo::getArticleNum($article_name);
            //获取章节内容url地址
            $article_url = \Common\Library\COM\CollectDo::getSubstr($val,'<a href="','" ">');
            //p($this->detailUrl.$article_url);exit;
            //获取章节内容并存储
            $contentPath = $this->contentCollection($this->detailUrl.$article_url);
            $articleDate = array(
                'book_id'=> $book_id,
                'article_name'=> $article_name,
                'create_time'=>  time(),
                'year'=>  date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'article_num'=> $article_num,
                'update_time' => time(),
                'unique'=>$unique,
                'piece_num'=> $piece_num,
                'piece_name'=> $piece_name,
                'content'=>$contentPath
            );
            //p($articleDate);//exit;
            $article_id = $this->BookArticle->add($articleDate);
            //p($this->BookArticle->getLastSql());exit;
            if(empty($article_id)){
                $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1));
                exit();
            }

            unset($articleList[$key]);
        }
        unset($this->BookArticle);
        $this->writeLog($this->detailUrl.'  本书入库完成!');
    }

    //章节内容入库
    protected function contentCollection($contentUrl = ''){
        if(empty($contentUrl)){
            return FALSE;
        }
        $savePath = './Runtime/Collect/xs74/content.html';
        \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
        $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        if(empty($contentA)){
            \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
            sleep(3);
            $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        }
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'<div id="content">','</div><script>read2();');

        if(!\Common\Library\COM\CollectDo::checkSpecialStr($contentA)){
            $data = array('id'=>$this->id,'disabled'=>4,'remark'=>'采集章节内容有误,包含特殊字符:'.$contentUrl);
            \HelloAdmin\Model\CrontabModel::updateData($data);
            $this->writeLog('采集章节内容有误,包含特殊字符:'.$contentUrl.'@@'.var_export($contentA,1),'ERR');
            exit();
        }

        $replace = array(
                'www.5du5.net',
                '5du5.net',
                '5du5');
        $contentT = str_replace($replace,'', $contentA);
        $contentT = htmlentities($contentT,ENT_COMPAT,'UTF-8');


        $contentPath = \Common\Library\COM\CollectDo::doContentPath();
        $handel = fopen('.'.$contentPath,'x+');
        if($handel && fwrite($handel,$contentT) !== FALSE){
            fclose($handel);
            return $contentPath;
        }
        $this->writeLog('章节内容存储失败!@'.var_export($contentPath,1).'|@|'.var_export($contentUrl,1));
        $this->writeLog('章节内容'.var_export($contentT,1));
        exit();
    }

    //采集日志记录
    private function writeLog($message,$level='INFO'){
        \Think\Log::write($message,$level,$type='','./Runtime/Collect/xs74/logs-'.  date('Ymd').'.log');
    }

    public function update(){
        $url = 'http://www.5du5.net/index.php';
        set_time_limit(0);
        $pathFile = './Runtime/Collect/xs74/updatelist.html';
        \Common\Library\COM\CollectDo::downPage($url, $pathFile);

        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $updatelist = \Common\Library\COM\CollectDo::getSubstr($getStr,'最近更新小说列表</h2>','<h2>最新入库小说');
        unset($getStr,$pathFile);
        //p($getStr);
        //获取书籍名称
        $article_preg = '/<li><span class="s1">(.+?)<\/span><\/li>/i';
        preg_match_all($article_preg, $updatelist, $articles);
        $articleList = $articles[1];
        unset($articles,$updatelist);
        //p($articleList);exit;
        $Book = new \Common\Model\BookModel();
        $Author = new \Common\Model\BookAuthorModel();

        foreach ($articleList as $key => $value) {
            $bookName = \Common\Library\COM\CollectDo::getSubstr($value,'" target="_blank">','</a></span><span class="s3');
            $authorName = \Common\Library\COM\CollectDo::getSubstr($value,'s4">','</span><span class="s5');

            if(empty($bookName) || empty($authorName)){
                $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
                exit('书名,作者,或为空!:'.var_export($value,1));
            }
            $author_id = $Author->getAuthorId($authorName);
            $unique = md5($bookName.$author_id);
            //书本重复检测
            $book_id = $Book->checkBookRepeart($unique);
            if($book_id > 0){
                $articleName = \Common\Library\COM\CollectDo::getSubstr($value,'.html" target="_blank">','</a></span><span class="s4');

                if(empty($articleName)){
                    $this->writeLog($articleName.' ====> 章节名称空','ERR');
                    exit();
                }
                $unique = md5($book_id.$articleName);
                $this->BookArticle = new \Common\Model\BookArticleModel();
                $result = $this->BookArticle->checkArtcileRepeat($unique);

                if($result){//重复章节检测
                    $this->writeLog($articleName.' ====> 重复章节');
                    continue;
                }
                //获取章节数
                $num = \Common\Library\COM\CollectDo::getArticleNum($articleName);
                $piece_num = \Common\Library\COM\CollectDo::getPieceNum($articleName);
                //获取章节内容地址
                $start = mb_strpos($value, '<span class="s3">',$end,'utf-8');
                $end = mb_strpos($value, 'target="_blank">',$end,'utf-8');
                $articleUrl = \Common\Library\COM\CollectDo::getSubstr($value,'s3','class="s4');
                $articleUrl = \Common\Library\COM\CollectDo::getSubstr($articleUrl,'"><a href="','" target="_blank">');
                //p($articleUrl);
                //获取章节内容并存储
                $contentPath = $this->contentCollection($articleUrl);
                $new = $this->BookArticle->getNewArticle($book_id);

                if(empty($num) || empty($new['article_num']) || $new['article_num']==($num-1)){//默认更新一个章节
                    $articleDate = array(
                        'book_id'=> $book_id,
                        'article_name'=> $articleName,
                        'create_time'=>  time(),
                        'year'=>  date('Y'),
                        'month'=>  date('m'),
                        'day'=>  date('d'),
                        'article_num'=> $num,
                        'update_time' => time(),
                        'unique'=>$unique,
                        'piece_num'=> $piece_num,
                        'piece_name'=> '',
                        'content'=>$contentPath
                    );
                    //p($articleDate);//exit;
                    $article_id = $this->BookArticle->add($articleDate);
                    $this->writeLog('====> 更新了 一个 章节 ======>');
                    continue;
                }else{//更新多个章节
                    $this->detailUrl  = \Common\Library\COM\CollectDo::getSubstr($value,'s2"><a href="','" target="_blank">');
                    $pathFile = './Runtime/Collect/xs74/updateaddbook.html';
                    \Common\Library\COM\CollectDo::downPage('http://www.5du5.net'.$this->detailUrl, $pathFile);
                    $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
                    $listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div id="list">','<div id="footer" name="footer">');
                    //p($listInfo);
                    //章节名称和篇名入库
                    $this->articleCollection($listInfo,$book_id);
                    $this->writeLog('====>  对比更新 多个 章节内容 =====>');
                }
            }
            $this->writeLog('====> 新书跳过，更新下一个章节 ====>');
            continue;
        }
    }

}
